package com.ruoyi.ebike.service.impl;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.ebike.domain.Coupon;
import com.ruoyi.ebike.domain.EbikeUser;
import com.ruoyi.ebike.mapper.CouponMapper;
import com.ruoyi.ebike.mapper.EbikeUserMapper;
import com.ruoyi.ebike.vo.CouponRecordVO;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.ebike.mapper.CouponRecordMapper;
import com.ruoyi.ebike.domain.CouponRecord;
import com.ruoyi.ebike.service.ICouponRecordService;

/**
 * 优惠券购买记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-01-07
 */
@Service
public class CouponRecordServiceImpl implements ICouponRecordService 
{
    @Autowired
    private CouponRecordMapper couponRecordMapper;
    @Autowired
    private CouponMapper couponMapper;
    @Autowired
    private EbikeUserMapper ebikeUserMapper;

    /**
     * 查询优惠券购买记录
     * 
     * @param id 优惠券购买记录主键
     * @return 优惠券购买记录
     */
    @Override
    public CouponRecord selectCouponRecordById(Long id)
    {
        return couponRecordMapper.selectCouponRecordById(id);
    }

    /**
     * 查询优惠券购买记录列表
     * 
     * @param couponRecordVO 优惠券购买记录
     * @return 优惠券购买记录
     */
    @Override
    public List<CouponRecordVO> selectCouponRecordVOList(CouponRecordVO couponRecordVO)
    {
//        return couponRecordMapper.selectCouponRecordList(couponRecordVO);
//        由于多了一个username字段查询 和 username字段展示
        return couponRecordMapper.selectCouponRecordVOList(couponRecordVO);
    }

    /**
     * 新增优惠券购买记录
     * 
     * @param couponRecord 优惠券购买记录
     * @return 结果
     */
    @Override
    public int insertCouponRecord(CouponRecord couponRecord)
    {
//        根据userId和couponId查询是否有数据
//        如果有 那么在原有的validCount上增加
//        如果没有数据 那么新建一条优惠券记录数据
        List<CouponRecord> couponRecords = couponRecordMapper.selectCouponRecordList(couponRecord);
        Coupon coupon = couponMapper.selectCouponById(couponRecord.getCouponId());
        EbikeUser ebikeUser = ebikeUserMapper.selectEbikeUserById(couponRecord.getUserId());
//        如果优惠券不存在 那么直接返回 不进行后续操作
        if(coupon==null){
            return -1;
        }
//        如果优惠券数量不够 那么直接返回 不进行后续操作
        if(coupon.getCount()<couponRecord.getValidCount()){
            return -1;
        }
//        如果用户不存在 那么直接返回 不进行后续操作
        if(ebikeUser==null){
            return -1;
        }
//        获取couponRecords的第一个数据，如果存在那么就把第一个数据的validCount加当前couponRecord的validCount
        if(couponRecords.size()>0){
            CouponRecord couponRecord1 = couponRecords.get(0);
            couponRecord1.setValidCount(couponRecord1.getValidCount()+couponRecord.getValidCount());
            coupon.setCount(coupon.getCount()-couponRecord.getValidCount());
            couponMapper.updateCoupon(coupon);
            return couponRecordMapper.updateCouponRecord(couponRecord1);
            }
//        否则新增优惠券记录
//            有效期计算
            Date createTime = coupon.getCreateTime();
            Integer cardTime = coupon.getCardTime();
            Calendar calendar= Calendar.getInstance();
            calendar.setTime(createTime);
            calendar.add(Calendar.DAY_OF_MONTH,cardTime);
            Date validTime = calendar.getTime();
//            手机号要根据userId查找
            String phoneNumber = ebikeUser.getPhoneNumber();
//            给coupon表的count减少validCount数目
            coupon.setCount(coupon.getCount()-couponRecord.getValidCount());
            couponMapper.updateCoupon(coupon);

//            把数据装配到couponRecord中
            couponRecord.setDiscount(coupon.getDiscount());
            couponRecord.setValidCount(couponRecord.getValidCount());
            couponRecord.setValidTime(validTime);
            couponRecord.setDateTime(new Date());
            couponRecord.setDeptId(coupon.getDeptId());
            couponRecord.setType(1);
            couponRecord.setPhone(phoneNumber);
            couponRecord.setMoney(coupon.getMoney());
            return couponRecordMapper.insertCouponRecord(couponRecord);

    }

    /**
     * 修改优惠券购买记录
     * 
     * @param couponRecord 优惠券购买记录
     * @return 结果
     */
    @Override
    public int updateCouponRecord(CouponRecord couponRecord)
    {
        return couponRecordMapper.updateCouponRecord(couponRecord);
    }

    /**
     * 批量删除优惠券购买记录
     * 
     * @param ids 需要删除的优惠券购买记录主键
     * @return 结果
     */
    @Override
    public int deleteCouponRecordByIds(Long[] ids)
    {
        return couponRecordMapper.deleteCouponRecordByIds(ids);
    }

    /**
     * 删除优惠券购买记录信息
     * 
     * @param id 优惠券购买记录主键
     * @return 结果
     */
    @Override
    public int deleteCouponRecordById(Long id)
    {
        return couponRecordMapper.deleteCouponRecordById(id);
    }
}
